package com.app.biz.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateException;
import cn.hutool.core.date.DateUtil;
import com.app.biz.domain.request.BizCityIndustrialEconomicsRequest;
import com.app.common.annotation.Anonymous;
import com.app.common.config.AppConfig;
import com.app.common.core.page.TableDataInfo;
import com.app.common.utils.file.FileUploadUtils;
import com.app.common.utils.poi.ExcelUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.app.common.annotation.Log;
import com.app.common.core.controller.BaseController;
import com.app.common.core.domain.AjaxResult;
import com.app.common.enums.BusinessType;
import com.app.biz.domain.BizCityIndustrialEconomics;
import com.app.biz.service.IBizCityIndustrialEconomicsService;
import org.springframework.web.multipart.MultipartFile;


/**
 * 分市工业企业主要经济指标Controller
 *
 * @author SangGuangCan
 * @date 2023-12-29
 */
@RestController
@RequestMapping("/biz/economics")
public class BizCityIndustrialEconomicsController extends BaseController
{
    private final IBizCityIndustrialEconomicsService bizCityIndustrialEconomicsService;

    @Autowired
    public BizCityIndustrialEconomicsController(IBizCityIndustrialEconomicsService bizCityIndustrialEconomicsService){
        this.bizCityIndustrialEconomicsService = bizCityIndustrialEconomicsService;
    }

    /**
     * 查询分市工业企业主要经济指标列表
     */
    @PreAuthorize("@ss.hasPermi('biz:economics:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizCityIndustrialEconomics bizCityIndustrialEconomics)
    {
        startPage();
        List<BizCityIndustrialEconomics> list = bizCityIndustrialEconomicsService.selectBizCityIndustrialEconomicsList(bizCityIndustrialEconomics);
        return getDataTable(list);
    }

    /**
     * 导出分市工业企业主要经济指标列表
     */
    @PreAuthorize("@ss.hasPermi('biz:economics:export')")
    @Log(title = "分市工业企业主要经济指标", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizCityIndustrialEconomics bizCityIndustrialEconomics)
    {
        List<BizCityIndustrialEconomics> list = bizCityIndustrialEconomicsService.selectBizCityIndustrialEconomicsList(bizCityIndustrialEconomics);
        ExcelUtil<BizCityIndustrialEconomics> util = new ExcelUtil<BizCityIndustrialEconomics>(BizCityIndustrialEconomics.class);
        util.exportExcel(response, list, "分市工业企业主要经济指标数据");
    }

    /**
     * 获取分市工业企业主要经济指标详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:economics:query')")
    @GetMapping(value = "/{baseId}")
    public AjaxResult getInfo(@PathVariable("baseId") String baseId)
    {
        return success(bizCityIndustrialEconomicsService.selectBizCityIndustrialEconomicsByBaseId(baseId));
    }

    /**
     * 新增分市工业企业主要经济指标
     */
    @PreAuthorize("@ss.hasPermi('biz:economics:add')")
    @Log(title = "分市工业企业主要经济指标", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizCityIndustrialEconomics bizCityIndustrialEconomics)
    {
        return toAjax(bizCityIndustrialEconomicsService.insertBizCityIndustrialEconomics(bizCityIndustrialEconomics));
    }

    /**
     * 修改分市工业企业主要经济指标
     */
    @PreAuthorize("@ss.hasPermi('biz:economics:edit')")
    @Log(title = "分市工业企业主要经济指标", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizCityIndustrialEconomics bizCityIndustrialEconomics)
    {
        return toAjax(bizCityIndustrialEconomicsService.updateBizCityIndustrialEconomics(bizCityIndustrialEconomics));
    }

    /**
     * 删除分市工业企业主要经济指标
     */
    @PreAuthorize("@ss.hasPermi('biz:economics:remove')")
    @Log(title = "分市工业企业主要经济指标", businessType = BusinessType.DELETE)
    @DeleteMapping("/{baseIds}")
    public AjaxResult remove(@PathVariable String[] baseIds)
    {
        return toAjax(bizCityIndustrialEconomicsService.deleteBizCityIndustrialEconomicsByBaseIds(baseIds));
    }

    /**
     * <p>
     *     查询APP左上角的日期数据
     * </p>
     * @return 日期的集合
     */
    @Anonymous
    @GetMapping("/getUniqueDatesFromDatabase")
    public AjaxResult getUniqueDatesFromDatabase(){
        List<String> dateArgs = bizCityIndustrialEconomicsService.getUniqueDatesFromDatabase();
        return AjaxResult.success(dateArgs);
    }

    /**
     * <p>
     *     通过日期查询表格中的数据
     * </p>
     * @param request 前传传来的数据
     * @return 表格中的数据
     */
    @Anonymous
    @GetMapping("/getEconomicIndicatorsByDateList")
    public AjaxResult getEconomicIndicatorsByDateList(BizCityIndustrialEconomicsRequest request){
        try{
            String dates = request.getDates();
            if(StringUtils.isBlank(dates)){
                return AjaxResult.warn("没有找到日期");
            }
            DateUtil.parse(request.getDates(),"yyyy年MM月");
            return AjaxResult.success(this.bizCityIndustrialEconomicsService.getEconomicIndicatorsByDateList(request));
        }catch (DateException de){
            logger.error("日期格式校验失败,传入日期:{},正确格式:{}",request.getDates(),"yyyy年MM月");
            return AjaxResult.error("日期校验失败");
        }catch (Exception e){
            logger.error("未知错误:{}",e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * <p>
     *     将临时表中的数据迁移到正式表虽
     * </p>
     * @return
     */
    @Anonymous
    @PostMapping("/transferDataInOneClick")
    public AjaxResult transferDataInOneClick(){
        return toAjax(bizCityIndustrialEconomicsService.transferDataInOneClick());
    }

    /**
     * <p>
     *     清空所有的分市工业企业主要经济指标数据
     * </p>
     * @return
     */
    @DeleteMapping("/clearAllTableData")
    public AjaxResult clearAllTableData(){
        bizCityIndustrialEconomicsService.clearAllTableData();
        return AjaxResult.success();
    }

    /**
     * <p>
     *     导入分市工业企业主要经济指标数据
     * </p>
     * @param params 前端传来的参数，目前只有一个月份
     * @param multipartFile 上传的Excel文件
     * @return 导入是否成功
     */
    @PostMapping("/importExcel")
    public AjaxResult importExcel(@RequestParam Map<String,String> params, @RequestPart("file") MultipartFile multipartFile){
        try{
            String yearMonth = params.get("yearMonth");
            if(StringUtils.isBlank(yearMonth)){
                return AjaxResult.warn("请选择导入的年月");
            }
            List<BizCityIndustrialEconomics> list = bizCityIndustrialEconomicsService.selectBizCityIndustrialEconomicsListNoParams();
            if(list != null && !list.isEmpty()){
                return AjaxResult.warn("导入前请先清空历史数据");
            }
            /* 上传文件的路径 */
            String uploadPath = FileUploadUtils.upload(AppConfig.getProfile(),multipartFile);
            String absoluteExcelPath = FileUploadUtils.replaceAbsolutePath(uploadPath);
            return bizCityIndustrialEconomicsService.importExcel(absoluteExcelPath,yearMonth);
        }catch (IOException e){
            return AjaxResult.error("导入失败");
        }
    }
}
